package com.fanix5.javamvvm.viewmodel.state;

import android.util.Log;

import com.fanix5.javamvvm.app.Constant;
import com.fanix5.javamvvm.base.BaseViewModel;
import com.fanix5.javamvvm.bean.BaseBean;
import com.fanix5.javamvvm.bean.DoctorBean;
import com.fanix5.javamvvm.bean.ListDataUiState;
import com.fanix5.javamvvm.event.EventLiveData;
import com.fanix5.javamvvm.network.BaseObserver;
import com.fanix5.javamvvm.network.RxSchedulers;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

public class DoctorListViewModel extends BaseViewModel {


    public final EventLiveData<ListDataUiState<List<DoctorBean>>> liveData = new EventLiveData<>();

    private int pageNo = 1;

    public void getData(boolean isRefresh){
        if(isRefresh){
            pageNo = 1;
        }


        getHttp().getTest(pageNo, Constant.BASE_PAGE_NO)
                .compose(RxSchedulers.applySchedulers())
                .subscribe(new BaseObserver<BaseBean<List<DoctorBean>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<DoctorBean>> result) {
                        pageNo++;
                        Logger.e("哈哈"+result);
                        List<DoctorBean> list = new ArrayList<>();
                        if(result.getData() != null){
                            list = result.getData();
                        }

                        liveData.setValue(new ListDataUiState<>(true,result.getMsg(),isRefresh,list.isEmpty(),list.size() == Constant.BASE_PAGE_NO,isRefresh && list.isEmpty(),list));
                    }

                    @Override
                    public void onFailure(String errMsg, boolean isNetError) {
                        List<DoctorBean> list = new ArrayList<>();
                        list.size();
                        liveData.setValue(new ListDataUiState<>(false,errMsg,isRefresh, true, false, isRefresh,list));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
                });


    }



}
